/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ppsa.dao;

import java.sql.*;
import javax.sql.*;

/**
 *
 * @author JONATAN
 */
public class AccesoDao extends AccesoDaoException {
    
    String stpv_nombreHOST = "127.0.0.1"; //"HP062040";
    String strpv_conexionUrl = "jdbc:sqlserver://"+stpv_nombreHOST+":1433;"
            + "databaseName=ppsa;user=sa; password=123456;";
//    String strpv_conexionUrl = "jdbc:sqlserver://HP062040:1433;"
//            + "databaseName=polinsumos;user=sa; password=123456;";
    // Declaramos los sioguientes objetos
    protected DataSource objPt_dataSource = null;
    protected Connection objPt_conn = null;
    protected ResultSet objPt_rs = null;
    protected Statement objPt_stmt = null;
    protected PreparedStatement objPt_pstmt = null;
    protected CallableStatement objPt_cstmt = null;
    protected String av_message;
    String storedProcedure;

    public AccesoDao() throws AccesoDaoException {

        try {

            //return (DataSource) c.lookup("java:comp/env/conexionPruebas");
            /*Properties prop = new Properties();
            prop.load(new FileInputStream("config.properties"));
            this.strpv_conexionUrl = "jdbc:sqlserver://"+stpv_nombreHOST+":1433;"
            + "databaseName="+prop.getProperty("database")+";user=sa; password=123456;";*/
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            
        } catch (ClassNotFoundException e) {
            throw new AccesoDaoException(
                    "Cannot locate data source at ", e);
        }
    }

    public AccesoDao(DataSource dataSource) {
        this.objPt_dataSource = dataSource;
    }

    public void closeResultSet(ResultSet rs)
            throws AccesoDaoException {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                throw new AccesoDaoException("SQL Exception ", e);
            }
        }
    }

    public void closeStatement(Statement stm)
            throws AccesoDaoException {
        if (stm != null) {
            try {
                if (stm.isClosed()) {
                    return;
                }
                stm.close();
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                throw new AccesoDaoException("SQL Exception ", e);
            }
        }
    }

    public void closePreparedStatement(PreparedStatement pstm)
            throws AccesoDaoException {
        if (pstm != null) {
            try {
                if (pstm.isClosed()) {
                    return;
                }
                pstm.close();
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                throw new AccesoDaoException("SQL Exception ", e);
            }
        }
    }

    public void closeCallableStatement(CallableStatement cstm)
            throws AccesoDaoException {
        if (cstm != null) {
            try {
                if (cstm.isClosed()) {
                    return;
                }
                cstm.close();
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                throw new AccesoDaoException("SQL Exception ", e);
            }
        }
    }

    public void closeConnection(Connection conn)
            throws AccesoDaoException {
        if (conn != null) {
            try {
                if (conn.isClosed()) {
                    return;
                }
                conn.close();
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                throw new AccesoDaoException("SQL Exception ", e);
            }
        }
    }

    public DataSource getDataSource() {
        return objPt_dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.objPt_dataSource = dataSource;
    }
}
